package com.su.lightthings.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseMultiItemAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import com.su.lightthings.R
import com.su.lightthings.bean.DeviceListBean
import com.su.lightthings.databinding.AdapterDeviceListAddDeviceBinding
import com.su.lightthings.databinding.AdapterDeviceListBinding

class DeviceListAdapter : BaseMultiItemAdapter<DeviceListBean>() {

    companion object {
        private const val DEVICE_TYPE = 0
        private const val ADD_TYPE = 1
    }

    class DeviceVH(val viewBinding: AdapterDeviceListBinding) : RecyclerView.ViewHolder(viewBinding.root)
    class AddVH(private val viewBinding: AdapterDeviceListAddDeviceBinding) : RecyclerView.ViewHolder(viewBinding.root)

    init {
        addItemType(
            DEVICE_TYPE,
            object : OnMultiItemAdapterListener<DeviceListBean, DeviceVH> {
                override fun onBind(holder: DeviceVH, position: Int, item: DeviceListBean?) {
                    item!!
                    holder.viewBinding.tvDeviceName.text = item.deviceName
                    holder.viewBinding.tvDeviceConnectState.text = item.deviceState
                    holder.viewBinding.tvDevicePower.text = item.power
                    holder.viewBinding.ivDeviceIcon.setImageResource(item.icon)
                }

                override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): DeviceVH {
                    return DeviceVH(AdapterDeviceListBinding.inflate(LayoutInflater.from(context), parent, false))
                }
            }
        ).addItemType(
            ADD_TYPE,
            object : OnMultiItemAdapterListener<DeviceListBean, AddVH> {
                override fun onBind(holder: AddVH, position: Int, item: DeviceListBean?) {

                }

                override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): AddVH {
                    return AddVH(AdapterDeviceListAddDeviceBinding.inflate(LayoutInflater.from(context), parent, false))
                }
            }
        ).onItemViewType { position, list ->
                if (position < list.size)
                    DEVICE_TYPE
                else
                    ADD_TYPE
            }
    }

    override fun getItemCount(items: List<DeviceListBean>): Int {
        return items.size + 1
    }
}